﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.IO;
using System.Web;
using System.Diagnostics;

namespace Card.Log
{
    public class Logger
    {
        static Logger()
        {
            log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(HttpContext.Current.Server.MapPath("~/log4net.config")));
        }

        #region Debuge
        /// <summary>
        /// Debug
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="message">信息</param>
        public static void Debug(Type type, string message)
        {
            ILog log = LogManager.GetLogger(type);
            log.Debug(message);
        }
        /// <summary>
        /// Log级别，Debug
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="message"></param>
        public static void Debug(string strName, string message)
        {
            ILog log = LogManager.GetLogger(strName);
            log.Debug(message);
        }
        /// <summary>
        /// Log级别，Debug
        /// </summary>
        /// <param name="message"></param>
        public static void Debug(string message)
        {
            Debug(GetStackType(), message);
        }
        #endregion

        #region Error
        /// <summary>
        /// Log级别，Error
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="message"></param>
        public static void Error(Type type, string message)
        {
            ILog log = LogManager.GetLogger(type);
            log.Error(message);
        }
        /// <summary>
        /// Log级别，Error
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="message"></param>
        public static void Error(string strName, string message)
        {
            ILog log = LogManager.GetLogger(strName);
            log.Error(message);
        }
        /// <summary>
        /// Log级别，Error
        /// </summary>
        /// <param name="message"></param>
        public static void Error(string message)
        {
            Error(GetStackType(), message);
        }
        #endregion

        #region private
        private static Type GetStackType()
        {
            StackTrace stack = new StackTrace(true);
            if (stack.FrameCount >= 2)
            {
                return stack.GetFrame(2).GetMethod().DeclaringType;
            }
            else
            {
                return typeof(Logger);
            }
        }
        #endregion
    }
}
